using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;

namespace Data.Models.Mapping
{
    public class ProductMap : EntityTypeConfiguration<Product>
    {
        public ProductMap()
        {
            // Primary Key
            this.HasKey(t => t.ID);

            // Properties
            this.Property(t => t.Code)
                .IsRequired()
                .HasMaxLength(40);

            this.Property(t => t.Name)
                .HasMaxLength(250);

            this.Property(t => t.Attachment)
                .HasMaxLength(250);

            this.Property(t => t.Description)
                .HasMaxLength(1000);

            this.Property(t => t.ImageDesign)
                .HasMaxLength(250);

            // Table & Column Mappings
            this.ToTable("Product");
            this.Property(t => t.ID).HasColumnName("ID");
            this.Property(t => t.Code).HasColumnName("Code");
            this.Property(t => t.Name).HasColumnName("Name");
            this.Property(t => t.Attachment).HasColumnName("Attachment");
            this.Property(t => t.ProductTypeID).HasColumnName("ProductTypeID");
            this.Property(t => t.Description).HasColumnName("Description");
            this.Property(t => t.CategoryID).HasColumnName("CategoryID");
            this.Property(t => t.ImageDesign).HasColumnName("ImageDesign");
            this.Property(t => t.ParentID).HasColumnName("ParentID");
            this.Property(t => t.Level).HasColumnName("Level");

            // Relationships
            this.HasOptional(t => t.Category)
                .WithMany(t => t.Products)
                .HasForeignKey(d => d.CategoryID);
            this.HasRequired(t => t.ProductType)
                .WithMany(t => t.Products)
                .HasForeignKey(d => d.ProductTypeID);

        }
    }
}
